<template>
    <div class="app-container">

        <el-form :inline="true" class="demo-form-inline caozuo">


            <el-form-item label="公告优先级" >
                <el-select v-model="Pvalue" placeholder="请选择" >
                    <el-option v-for="item in prioritys" :key="item.Pvalue" :label="item.label" :value="item.Pvalue">
                    </el-option>
                </el-select>
            </el-form-item>

            
            <el-form-item>
                <el-input v-model="searchObj.title" placeholder="请输入公告标题" />
            </el-form-item>

            <el-form-item label="公告时间范围">
                <el-date-picker type="datetime" v-model="searchObj.beginTime" placeholder="选择开始时间"
                    value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" />
            </el-form-item>
            <el-form-item>
                <el-date-picker type="datetime" v-model="searchObj.endTime" placeholder="选择截止时间"
                    value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" />
            </el-form-item>


            <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
        </el-form>

        <!-- 表格 -->
        <el-table v-loading="listLoading" :data="list" element-loading-text="数据加载中" border fit highlight-current-row
            row-class-name="myClassList">

            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column label="公告图片" align="center">
                <template slot-scope="scope">
                    <div class="pic">
                        <!-- static\课程图片.png -->
                        <img :src="scope.row.image" width="150px">
                    </div>
                </template>
            </el-table-column>


            <el-table-column label="公告标题" align="center">
                <template slot-scope="scope">
                    {{ scope.row.title }}
                </template>
            </el-table-column>

            <el-table-column label="优先级" align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.priority === 1">一级公告</i>
                    <i v-else-if="scope.row.priority === 2">二级公告</i>
                    <i v-else-if="scope.row.priority === 3">三级公告</i>
                </template>
            </el-table-column>


            <el-table-column label="发布时间" width="300" align="center">
                <template slot-scope="scope">
                    {{ scope.row.gmtCreate }}
                </template>
            </el-table-column>



            <el-table-column label="点击阅读" width="400" align="center">
                <template slot-scope="scope">
                    <div v-if="scope.row.readFlag == '0'">
                        <a :href="`/my/info/${ scope.row.id }`">
                            <el-button type="warning" size="big">没有阅读</el-button>
                        </a>
                    </div>
                    <div v-if="scope.row.readFlag == '1'">
                        <a :href="`/my/info/${ scope.row.id }`">    
                            <el-button type="success" size="big">已经阅读</el-button>
                        </a>
                    </div>
                </template>
            </el-table-column>


        </el-table>

        <!-- 分页 -->
        <el-pagination :current-page="page" :page-size="limit" :total="total" style="padding: 30px 0; text-align: center;"
            layout="total, prev, pager, next, jumper" @current-change="fetchData" />
    </div>
</template>
<script>
import announcement from '@/api/announcement'

export default {

    data() {
        return {
            listLoading: true, // 是否显示loading信息
            list: null, // 数据列表
            total: 0, // 总记录数
            page: 1, // 页码
            limit: 5, // 每页记录数
            searchObj: {
                priority: '',
                title: '',
                beginTime: '',
                endTime: '',
            }, // 查询条件

            // 公告优先级选择
            prioritys: [{
                Pvalue: '1',
                label: '一级公告'
            }, {
                Pvalue: '2',
                label: '二级公告'
            }, {
                Pvalue: '3',
                label: '三级公告'
            }],
            Pvalue: '',
        }
    },

    created() {
        // 当页面加载时获取数据
        this.fetchData()
    },
    methods: {
        // 点击查询按钮执行的方法
        fetchData(page = 1) { // 调用api层获取数据库中的数据
            // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
            // 解决分页无效问题
            this.page = page
            this.listLoading = true
            this.searchObj.priority = this.Pvalue;
            // 这里的用户id固定写，后面再改
            announcement.selectListAnno('1080736474267144193', this.page, this.limit, this.searchObj).then(response => {
                // console.log('1:'+response.data.success)
                // debugger 设置断点调试
                if (response.data.success === true) {
                    this.list = response.data.data.rows
                    this.total = response.data.data.total
                    // console.log(this.list)
                    // console.log(this.total)
                }
                this.listLoading = false
            })
        },
        resetData() {
            this.searchObj = {}
            this.Pvalue = ''
            this.fetchData()
        },
    }
}
</script>

<style scoped>
.myClassList .info {
    width: 450px;
    overflow: hidden;
}

.myClassList .info .pic {
    width: 150px;
    height: 90px;
    overflow: hidden;
    float: left;
}

.myClassList .info .pic a {
    display: block;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

.myClassList .info .pic img {
    display: block;
    width: 100%;
}

.myClassList td .info .title {
    width: 280px;
    float: right;
    height: 90px;
}

.myClassList td .info .title a {
    display: block;
    height: 48px;
    line-height: 24px;
    overflow: hidden;
    color: #00baf2;
    margin-bottom: 12px;
}

.myClassList td .info .title p {
    line-height: 20px;
    margin-top: 5px;
    color: #818181;
}

.caozuo {
    /* transform: scale(0.8); */
}
</style>

   
